#coding=utf8
from google.appengine.ext.webapp import template
from google.appengine.ext import webapp
import unittest, os
from time import time
from common import *

import task

'''
Test case for 
'''

class FaintTask(webapp.RequestHandler):
    def get(self, params):
        if params == 'test':
            UTaskTest(self.response)
        else:
            self.response.out.write('Cool!')

class ChuanStr(object):
    def __init__(self):
        self.buf = ''
    def write(self, strbuf):
        self.buf += strbuf
        
class UTaskTest(webapp.RequestHandler):
    def __init__(self, response):
        self.response = response
        self.content = ''
        self.run()
        
    def run(self):
        chuanstrwriter = ChuanStr()
        runner = unittest.TextTestRunner(chuanstrwriter)
        testcastlist = []
        
        funclist1 = [
            'testCreateNewTaskNone',
            'testCreateNewTask',
            'testCreateNewTaskFail',
            'testGetTask',
            'testChangeName',
            'testChangeDueTime'
            ]
        [testcastlist.append(TaskTest(i)) for i in funclist1]

        [runner.run(i) for i in testcastlist]
        
        self.appendstr(chuanstrwriter.buf.replace('\n', '<br />'))
        self.output_content()

    def output_content(self):
        template_values = {
            'content' : self.content
            }
        path = os.path.join(os.path.dirname(__file__), './', 'templates', 'test.html')
        self.response.out.write(template.render(path, template_values))

    def appendstr(self, strinfo):
        self.content += strinfo
        self.content += '<br />'
    
    def appenderrstr(self, strinfo):
        self.content += '<font color="#FF0000">'
        self.content += strinfo
        self.content += '</font>'
        self.content += '<br />'

class TaskTest(unittest.TestCase):
    "test case for task"
    def testCreateNewTaskNone(self):
        nt = task.SpikeTask(u'chuan')
        assert nt.data_entity == None
        assert not nt.change_name('hello')

    def testCreateNewTaskFail(self):
        nt = task.SpikeTask(u'chuan')
        assert nt.data_entity == None
        assert nt.init_task([1, 0, 0]) == False
        assert nt.init_task([u'lala', -1, 0]) == False
        assert nt.init_task([u'lala', 0, -1]) == False
        assert not nt.change_name(u'hello')

    def testCreateNewTask(self):
        nt = task.SpikeTask(u'chuan')
        assert nt.init_task([u'testCreateNewTask', int(time()), int(time())])
        assert nt.save()
        assert nt.data_entity
        assert nt.key
        assert str(nt.key) == nt.data_entity.task_id

    def testGetTask(self):
        self.addatask()
        tt = task.get_all_task()[0]
        gt = task.SpikeTask(task_id=tt.task_id)
        assert gt.data_entity.task_id == tt.task_id
        assert gt.data_entity.description == tt.description
        assert gt.data_entity.tasknode_data == tt.tasknode_data
        assert gt.data_entity.owner == tt.owner
        assert gt.data_entity.state == tt.state

    def testChangeName(self):
        self.addatask()
        tt = task.get_all_task()[0]
        gt = task.SpikeTask(task_id=tt.task_id)
        assert gt.change_name(u'lalalala')
        assert gt.save()
        assert len(gt.tasknode_list) == 2

        ngt = task.SpikeTask(task_id=tt.task_id)
        assert ngt.tasknode_list == gt.tasknode_list
        assert ngt.get_current_tasknode()[0] == u'lalalala'

    def testChangeDueTime(self):
        self.addatask()
        tt = task.get_all_task()[0]
        gt = task.SpikeTask(task_id=tt.task_id)
        t = time()
        assert gt.change_duetime(int(t))
        assert gt.save()
        assert len(gt.tasknode_list) > 2

        ngt = task.SpikeTask(task_id=tt.task_id)
        assert ngt.tasknode_list == gt.tasknode_list
        assert ngt.get_current_tasknode()[1] == int(t)

    def setUp(self):
        pass
        
    def tearDown(self):
        task.clean_all_task(u'chuan')

    def addatask(self):
        nt = task.SpikeTask(u'chuan')
        assert nt.init_task([u'testCreateNewTask', 0, 0])
        assert nt.save()
